<?php
abstract class DALBase {

    /**
     * Database connection
     * @var <type>
     */
    protected $_connection = null;
    /**
     * Name of table
     * @var string
     */
    protected $_tbl = null;
    /**
     * Name of primary key column
     * @var string
     */
    protected $_key = null;
    
    protected $_config = array();

    /**
     * Default Init
     */
    public function __construct() {
        $this->_tbl = 'table_name';
        $this->_key = 'primary_key';
        $this->_config = Config::getData();
    }
    
    /**
     * Gets the connection
     * 
     * @return <type>
     */
    public function _getConnection() {
            if (!$this->_connection = mysql_connect($this->_config['host'], $this->_config['username'], $this->_config['password'])) {
		trigger_error('Error: Could not make a database link using ' . $this->_config['username'] . '@' . $this->_config['password']);
            }            
            if (!mysql_select_db($this->_config['database'], $this->_connection)) {
		trigger_error('Error: Could not connect to database ' . $this->_config['database']);
            }
            
            mysql_query("SET NAMES 'utf8'", $this->_connection);
            mysql_query("SET CHARACTER SET utf8", $this->_connection);
            mysql_query("SET CHARACTER_SET_CONNECTION=utf8", $this->_connection);
            mysql_query("SET SQL_MODE = ''", $this->_connection);
        
        return $this->_connection;
    }
    
    public function closeDB($connection){
        if($this->_connection != null){
            mysql_close();
        }
    }

    /**
     * Executes the query
     */
    public function executeQuery($query) {
        $conn = $this->_getConnection();
        return mysql_query($query, $conn);
    }

    /**
     * Loads item by primary key
     * 
     * @param mixed $id
     * @return object
     */
    public function getByID($id) {
        $item = -1;
        $query = "SELECT * FROM $this->_tbl WHERE $this->_key = $id";

        //
        $conn = $this->_getConnection();
        $result = $this->executeQuery($query);

        if($result){
            $item = mysql_fetch_array($result);
        }
        $this->closeDB($conn);
        
        return $item;
    }
    
    public function deleteByID($id){
        //
        $query = "DELETE FROM $this->_tbl WHERE $this->_key = $id";

        //
        $conn = $this->_getConnection();
        $result = $this->executeQuery($query);

        //
        $item = mysql_fetch_object($query);
        $this->closeDB($conn);
        //
        return $item;
    }
    
    public function getByProperty($propName = '1', $propVal = '1'){
        $result = array();
        
        $query = "SELECT * FROM $this->_tbl WHERE $propName='$propVal'";
        $data = $this->executeQuery($query);
        if($data){
            while ($row = mysql_fetch_object($data)) {
                //  put row object into the array
                $result[] = $row;
            }
        }
        
        return $result;
    }

    /**
     * Loads a list
     *
     * @return array
     */
    public function loadList($orderBy='', $order = '', $p = 1, $perPage = 20) {
        $data = array();
        $p_start = ($p == 1) ? 0 : ($p-1) * $perPage;
        
        //
        $query = "SELECT * FROM $this->_tbl";
        if($orderBy){
            $query .= " ORDER BY $orderBy ";
            
            $query .= ($order) ? $order : ' DESC ';
        }
        if($p != -1){
            $query .= ' LIMIT ' . $p_start . ", $perPage";
        }
//        echo $query;    
        $result = $this->executeQuery($query);
        if ($result) {
            //  Scan through the resource
            while ($row = mysql_fetch_object($result)) {
                //  put row object into the array
                $data[] = $row;
            }
        }
        //
        return $data;
    }
    
    public function count($where = '', $orderBy='', $order = ''){
        $row = 0;
        
        $query = "SELECT count(*) as countData FROM $this->_tbl ";
        if($where){
            $query .=  " $where ";
            if($orderBy){
                $query .= " $orderBy ";
                if($order){
                    $query .= " $order ";
                }
            }
        }
//        echo $query;
        $result = $this->executeQuery($query);
        if ($result) {
            $data = mysql_fetch_assoc($result);
            $row = (int) $data['countData'];
        }
        return $row;
    }
}